import { queryCompanies } from '@/pages/SystemUser/service';
import { Role } from '@/utils/typings.d';
import { PlusOutlined } from '@ant-design/icons';
import { ModalForm, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-form';
import { Button, message } from 'antd';
import { useModel } from 'umi';
import { createIotSim } from '../service';
import type { CreateIotSimType } from '../typings.d';

const CreateIotSim = (props: { reload: () => void }) => {
  const { initialState } = useModel('@@initialState');
  const currUser = initialState?.currentUser;
  const role = currUser?.role;
  return (
    <ModalForm<CreateIotSimType>
      title="添加流量卡"
      layout="horizontal"
      width="40vw"
      modalProps={{
        destroyOnClose: true,
      }}
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      trigger={
        <Button type="primary">
          <PlusOutlined />
          添加流量卡
        </Button>
      }
      onFinish={async (values) => {
        const res = await createIotSim(values);
        if (res.success) {
          message.success({
            content: res.message,
            key: 'add_iotsim',
          });
          props.reload();
          return true;
        }
        message.error({
          content: res.message,
          key: 'add_iotsim',
        });
        return false;
      }}
    >
      <ProFormText required label="流量卡号" placeholder="请输入流量卡号" name="iccid" />
      <ProFormSelect
        label="设备类型"
        required
        placeholder="请选择设备类型"
        name="deviceType"
        options={[
          {
            label: '视频巡检车',
            value: 0,
          },
          {
            label: 'GPS设备',
            value: 1,
          },
          {
            label: '4G车位锁',
            value: 2,
          },
          {
            label: '4G流量主机',
            value: 3,
          },
          {
            label: '诱导屏控制卡',
            value: 4,
          },
          {
            label: '其他设备',
            value: 99,
          },
        ]}
      />
      <ProFormSelect
        label="卡供应商"
        name="provider"
        placeholder="请选择卡供应商"
        options={[
          {
            label: '齐犇',
            value: 'tibiot',
          },
          {
            label: '量讯',
            value: 'upiot',
          },
          {
            label: '大汉三通',
            value: 'dahan',
          },
        ]}
      />
      <ProFormTextArea label="备注" name="application" placeholder="请填写本流量卡具体用途" />
      {role === Role.ADMIN && (
        <ProFormSelect
          name="ownerId"
          label="归属公司"
          showSearch
          debounceTime={200}
          rules={[{ required: true, message: '请选择流量卡所属' }]}
          placeholder="请选择流量卡所属公司(如未找到目标公司，请优先创建[企业管理者])"
          request={(val) => queryCompanies(val.keyWords, true)}
        />
      )}
    </ModalForm>
  );
};

export default CreateIotSim;
